import {
    findOutApplyById,
    getUserName,
    getDefResGoods,
    saveNewOutApply,
    findOutGoodWithApplyId,
    alterOutApply
} from "../../../api/afftersale/refacout";

let outApplyInfo = {
    data: function () {
        return {
            //修改或查看时的id
            id: 0,
            //标识状态 0 alter ;1 show ;2 add
            type: "",
            //创建人
            createBy: "",
            //残次品库
            goodList: [],
            //分页信息
            currentPage: 1,
            pageSize: 5,
            total: 0,

            //要添加的商品列表
            selectedGoodList: [],
            //存储修改时，本身存在的商品列表
            goodTempList: [],
            //返厂地点
            refacLocation: "",
            //返厂原因
            refacReason: "",
            //备注
            comments: "",
            //审核信息
            approver: "",
            approverTime: "",
            approverComments: "",
            approveStatus: "",
            formatApproveTime: "",
            odoCode: "",

            dialogTableVisible: false,//模态框可见性

            //模态框搜索（残品库）
            search: {
                currentPage: 1,
                pageSize: 5,
                goodName: "",//商品名
                goodImei: "",//商品串号
                goodBrand: "",//商品品牌
                goodModel: "",//商品型号
            }
        }
    },
    methods: {
        //获取当前登录用户名
        getUser() {
            let vm = this;
            getUserName().then(value => {
                vm.createBy = value;
            })
        },

        findById(id) {
            let vm = this;
            findOutApplyById(id).then(value => {
                console.log(111111111111);
                vm.refacLocation = value.refacLocation;
                vm.refacReason = value.refacReason;
                vm.comments = value.comments;
                vm.approver = value.approver;
                vm.approveStatus = value.approveStatus;
                vm.approverComments = value.approverComments;
                vm.approverTime = value.approverTime;
                vm.odoCode = value.odoCode;
                vm.formatApproveTime = vm.formatter(value.approverTime)
            });
            //获得对应申请单id的商品信息

            findOutGoodWithApplyId(id).then(value => {
                if (value === undefined)
                    return;
                vm.selectedGoodList = value;
                value.forEach(a => {
                    vm.goodTempList.push(a)
                });
            });
            console.log(this.goodTempList)
        },

        //打开模态框
        openAdd() {
            this.dialogTableVisible = true;
        },

        //获取残品库中商品
        getGoods() {
            let vm = this;
            getDefResGoods(this.search).then(value => {
                vm.goodList = value.records;
                vm.total = value.total;
            });

        },

        //搜索
        findPage() {
            this.getGoods();
        },

        //重置搜索
        resetForm() {
            this.search = {
                currentPage: 1,
                pageSize: 5,
                goodName: "",//商品名
                goodImei: "",//商品串号
                goodBrand: "",//商品品牌
                goodModel: "",//商品型号
            };
            this.getGoods();
        },

        //格式化时间
        formatter(row) {
            if (row != null)
                return row.replace("T", " ");
        },

        //每页条数改变
        handleSizeChange(pageSize) {
            this.search.pageSize = pageSize;
            this.pageSize = pageSize;
            this.getGoods();
        },

        //当前页改变
        handleCurrentChange(currentPage) {
            this.search.currentPage = currentPage;
            this.currentPage = currentPage;
            this.getGoods();
        },

        //模态框选择该表
        handleSelectionChange(val) {
            if (this.type !== '0') {
                this.selectedGoodList = val;
            } else {
                //修改时。
                this.goodTempList.forEach(value => {
                    val.push(value);
                });
                this.selectedGoodList = val;
            }
        },

        //删除选择的商品
        delFromList(row) {
            // console.log(row);
            let flag = false;
            for (let i = 0; i < this.selectedGoodList.length; i++) {
                if (this.selectedGoodList[i].id === row.id) {
                    var index = i;
                    flag = true;
                }
            }
            if (flag) {
                this.selectedGoodList.splice(index, 1);
            }
            flag = false;
            //检查临时存储数组
            for (let i = 0; i < this.goodTempList.length; i++) {
                if (this.goodTempList[i].id === row.id) {
                    var index2 = i;
                    flag = true;
                }
            }
            if (flag) {
                console.log(11);
                this.goodTempList.splice(index2, 1);
            }
            // 若goodlist中存在，则更改模态框内的选中情况
            let containsFlag = false;
            this.goodList.forEach(value => {
                if (row.id === value.id) {
                    containsFlag = true;
                }
            });
            if (!containsFlag) {
                return;
            }
            this.$refs.multipleTable.toggleRowSelection(row);
        },

        //保存新增申请
        saveNewApply() {
            let vm = this;
            let idList = [];
            this.selectedGoodList.forEach(val => {
                idList.push(val.id)
            });
            let params = {
                idList: idList,
                refacLocation: vm.refacLocation,
                refacReason: vm.refacReason,
                comments: vm.comments,
                createBy: vm.createBy
            };
            saveNewOutApply(params).then(value => {
                this.$message.success(value);
                this.returnMan();
            }).catch((e) => {
                console.log(e)
            })
        },

        //修改申请
        alterApply() {
            let vm = this;
            let idList = [];
            this.selectedGoodList.forEach(val => {
                idList.push(val.id)
            });
            let params = {
                id: vm.id,
                idList: idList,
                refacLocation: vm.refacLocation,
                refacReason: vm.refacReason,
                comments: vm.comments,
                createBy: vm.createBy,
                odoCode: vm.odoCode
            };
            alterOutApply(params).then(value => {
                this.$message.success(value);
                this.returnMan();
            }).catch((e) => {
                console.log(e)
            })
        },

        //返回管理页面
        returnMan() {
            this.$router.push({path: '/refacoutman'});
        },
    },
    created() {
        let vm = this;
        let id = this.$route.query.id;
        let flag = this.$route.query.flag;
        if (id !== undefined) {
            //进行axios，查询对应的id
            vm.id = id;
            vm.findById(id);
        }
        if (flag !== undefined) {
            vm.type = flag;
            console.log(vm.type)
        }
        this.getUser();
        this.getGoods();
    }
};

export default outApplyInfo;